package com.ghost.UI
{
	import com.ghost.model.UI_skin_data;
	
	import fl.text.TLFTextField;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.media.SoundTransform;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import flashx.textLayout.edit.EditManager;

	/**
	 * @author ghost
	 * 富文本
	 */
	public class Ghost_TLF_txt extends Sprite
	{
		private var scroll:Ghost_scroll;//滚动条
		private var txt:TLFTextField;//文本
		private var txt_width:Number;//文本宽
		private var txt_height:Number;//文本高
		private var scroll_skin_v:Vector.<Object>;//滚动条skin皮肤数据
		/**
		 * @param width 宽度
		 * @param height 高度
		 * @param skin_v 滚动条皮肤
		 * @param sound 滚动条声音
		 */			
		public function Ghost_TLF_txt(width:Number=100,height:Number=100,skin_v:Vector.<Object>=null)
		{
			if(skin_v!=null)scroll_skin_v = skin_v;
			else scroll_skin_v = UI_skin_data.ghost_scroll_skin_v;
			txt_width = width;
			txt_height = height;
			txt = new TLFTextField();
			this.addChild(txt);
			txt.wordWrap = true;
			txt.width = width;
			txt.height = height;
		}
		/**
		 * @param textformat TextFormat 对象
		 * @param start_n 开始的索引位置
		 * @param end_n 结束的索引位置
		 */		
		public function setTextFormate(textformat:TextFormat,start_n:int=-1,end_n:int = -1):void
		{
			txt.setTextFormat(textformat,start_n,end_n);
		}
		/**
		 * @param str 设置普通文本数据
		 */		
		public function set text(str:String):void
		{
			txt.text = str;
			update();
		}
		/**
		 * @param str html数据格式文本
		 */		
		public function set htmlText(str:String):void
		{
			txt.htmlText = str;
			update();
		}
		public function get textHeight():Number
		{
			return txt.textHeight;
		}
		/**
		 * @param h 文本高度
		 */		
		public function set txtheight(h:Number):void
		{
			txt.height = h;
			//this.height = h;
		}
		/**
		 * @param w 文本宽度
		 */		
		public function set txtwidth(w:Number):void
		{
			txt.width = w;
			//this.width = w;
		}
		/**
		 * @bool 是否需要边框
		 */		
		public function set border(bool:Boolean):void
		{
			txt.border = bool;
		}
		/**
		 *设置文本对齐方式 
		 * @param type
		 */		
		public function set autoSize(type:String):void
		{
			txt.autoSize = type;
		}
		/**
		 *设置文本类型 
		 * @param type
		 */		
		public function set txt_type(type:String):void
		{
			txt.type = type;
		}
		/**
		 *文本是否可选 
		 * @param bool
		 */		
		public function set selectable(bool:Boolean):void
		{
			txt.selectable = bool;
		}
		/**
		 * @param bool 是否换行
		 */		
		public function set wordWrap(bool:Boolean):void
		{
			txt.wordWrap = bool;
		}
		/**
		 * @param str 增加文本
		 */		
		public function appendText(str:String):void
		{
			txt.appendText(str);
		}
		/**
		 * @param o 插入对象 mc等等 
		 * @param o_w 插入对象的宽
		 * @param o_h 插入对象的高
		 * @param start_n 替换的初始位置索引
		 * @param end_n 替换的结束位置索引，但不包括此位置的字符
		 * (mc,2,4)代表将第三个字符到第五个字符（不包括第五个）替换为mc;(mc,2,2)代表将在mc插入到第三个字符的位置 但不替换.
		 */		
		public function insert_object(o:Object,o_w:Object,o_h:Object,start_n:int,end_n:int):void
		{
			if(o != null)
			{
				EditManager(txt.textFlow.interactionManager).selectRange(start_n,end_n);
				EditManager(txt.textFlow.interactionManager).insertInlineGraphic(o,o_w,o_h,null);
				//txt.textFlow.flowComposer.updateAllControllers();
				update();
			}
		}
		/**
		 *更新滚动条
		 */		
		public function update():void
		{
			var text_height:Number = txt.textHeight;
			if(text_height>txt_height)
			{
				if(scroll)scroll.visible = true;
				else
				{
					scroll = new Ghost_scroll(scroll_skin_v);
					this.addChild(scroll);
				}
				scroll.height = txt_height;
				scroll.scrollTarget = txt;
				scroll.move(txt.x+txt.width,txt.y);
				//scroll.update();
			}
			else
			{
				if(scroll != null)
				{
					scroll.visible = false;
				}
			}
		}
	}
}